[Sublist] Write approaches#3439
Conversation
I don't think these qualify as magic values, and I think we should remove all reference to it. There are better places/exercises to show the dangers of magic values to a student. We can leave them off here.
You tell me 😄 Have you run If you haven't, you should not assert that one approach is faster than another. You can take a look at some of the approaches that include performance articles to see what others have done ... but also be careful to make sure you have good test data. Ideally, you'd use various combinations of what the tests use, or other combinations that call out where, for example indexing would be slow or
I haven't dug into all the details yet, but will make suggestions when I do.
Simpler doesn't mean equivalent, as you can see from Isaacs comments above. 🙂 However, he is incorrect that string conversion in general is fundamentally flawed. It's not the string conversion that is a problem here -- its how the string representation is made. Simply doing There is another problem with the stated string solution. It assumes that if something is not a SUBLIST, it is automagically a SUPERLIST. But that's not the case. So you have to check each scenario separately: def sublist(list_one, list_two):
first = ','.join(str(item) for item in list_one)
second = ','.join(str(item) for item in list_two)
if list_one == list_two:
return EQUAL
if len(list_one) < len(list_two):
if first in second:
return SUBLIST
else:
if second in first:
return SUPERLIST
return UNEQUALThe code above passes all the tests on the site as well as the two additional tests Isaac notes above. You can walk through this solution as well as my simplified one on Python Tutor Now, there may be more corner cases that then fail - and I think we should note that that could be the case, and caution that using this methodology could be brittle -- but I think we should include the edited version as a solution that does pass all the tests (plus a few extra). |
BethanyG
left a comment
There was a problem hiding this comment.
@safwansamsudeen -- Please see comments above. I want the string approach included (with edits) as valid. Thanks.
|
Any string encoding has the issue that you can encode one list and add that as an element to the other list. Using >>> sublist(["a", "b"],["a,b"])
'SUPERLIST'More generally, if you have: first = str_encode(list_one)
second = str_encode(list_two)
if second in first:
return SUPERLISTThen you can do Another issue with |
|
Why was this merged? Should I open another PR with the suggested changes - or is the string based approach wrong as Isaac says? |
|
@safwansamsudeen -- I've given up arguing. We can leave it as wrong. It doesn't generalize to all possibilities, so we can let Isaac have this one. If you think there should be more additions/changes you can submit a new PR. |
Few points on my this:
+ ", "b) or is this perfectly fine c) or is the solution fundamentally problematic?Hope I haven't made any formatting issues this time, and thanks!